# MindMate Development Makefile
# 开发环境快速操作命令

.PHONY: help setup install dev build test clean docker-up docker-down migrate lint format

# 默认目标
.DEFAULT_GOAL := help

# 颜色定义
RED=\033[0;31m
GREEN=\033[0;32m
YELLOW=\033[0;33m
BLUE=\033[0;34m
NC=\033[0m # No Color

help: ## 显示帮助信息
	@echo "$(BLUE)MindMate - AI学习伙伴系统$(NC)"
	@echo "$(YELLOW)可用命令:$(NC)"
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "  $(GREEN)%-15s$(NC) %s\n", $$1, $$2}'

setup: ## 初始化开发环境
	@echo "$(BLUE)🚀 初始化MindMate开发环境...$(NC)"
	@cp .env.example .env || echo "$(YELLOW)⚠️ .env文件已存在$(NC)"
	@echo "$(GREEN)✅ 环境文件已创建，请编辑 .env 填入配置$(NC)"

install: ## 安装所有依赖
	@echo "$(BLUE)📦 安装前后端依赖...$(NC)"
	@echo "$(YELLOW)安装后端依赖...$(NC)"
	cd backend && pip install -r requirements.txt
	@echo "$(YELLOW)安装前端依赖...$(NC)"
	cd frontend && npm install
	@echo "$(GREEN)✅ 依赖安装完成$(NC)"

dev: ## 启动开发服务器
	@echo "$(BLUE)🔥 启动开发服务器...$(NC)"
	@echo "$(YELLOW)启动后端服务器 (localhost:8000)...$(NC)"
	cd backend && python -m uvicorn app.main:app --reload --host 0.0.0.0 --port 8000 &
	@sleep 3
	@echo "$(YELLOW)启动前端开发服务器 (localhost:3000)...$(NC)"
	cd frontend && npm run dev

dev-backend: ## 仅启动后端服务器
	@echo "$(BLUE)🔧 启动后端服务器...$(NC)"
	cd backend && python -m uvicorn app.main:app --reload --host 0.0.0.0 --port 8000

dev-frontend: ## 仅启动前端开发服务器
	@echo "$(BLUE)⚛️ 启动前端开发服务器...$(NC)"
	cd frontend && npm run dev

build: ## 构建生产版本
	@echo "$(BLUE)🔨 构建生产版本...$(NC)"
	@echo "$(YELLOW)构建前端...$(NC)"
	cd frontend && npm run build
	@echo "$(YELLOW)构建后端...$(NC)"
	cd backend && pip install --upgrade pip setuptools wheel
	@echo "$(GREEN)✅ 构建完成$(NC)"

test: ## 运行测试
	@echo "$(BLUE)🧪 运行测试...$(NC)"
	@echo "$(YELLOW)运行后端测试...$(NC)"
	cd backend && pytest -v --cov=app
	@echo "$(YELLOW)运行前端测试...$(NC)"
	cd frontend && npm run test
	@echo "$(GREEN)✅ 测试完成$(NC)"

test-backend: ## 仅运行后端测试
	@echo "$(BLUE)🧪 运行后端测试...$(NC)"
	cd backend && pytest -v --cov=app

test-frontend: ## 仅运行前端测试
	@echo "$(BLUE)🧪 运行前端测试...$(NC)"
	cd frontend && npm run test

lint: ## 运行代码检查
	@echo "$(BLUE)🔍 运行代码检查...$(NC)"
	@echo "$(YELLOW)检查后端代码...$(NC)"
	cd backend && python -m flake8 app/
	cd backend && python -m mypy app/
	@echo "$(YELLOW)检查前端代码...$(NC)"
	cd frontend && npm run lint
	cd frontend && npm run type-check
	@echo "$(GREEN)✅ 代码检查完成$(NC)"

format: ## 格式化代码
	@echo "$(BLUE)✨ 格式化代码...$(NC)"
	@echo "$(YELLOW)格式化后端代码...$(NC)"
	cd backend && python -m black app/
	cd backend && python -m isort app/
	@echo "$(YELLOW)格式化前端代码...$(NC)"
	cd frontend && npm run format
	@echo "$(GREEN)✅ 代码格式化完成$(NC)"

# ================================
# 数据库操作
# ================================

migrate: ## 运行数据库迁移
	@echo "$(BLUE)🗄️ 运行数据库迁移...$(NC)"
	cd backend && python -m alembic upgrade head
	@echo "$(GREEN)✅ 数据库迁移完成$(NC)"

migrate-auto: ## 自动生成数据库迁移
	@echo "$(BLUE)🗄️ 自动生成数据库迁移...$(NC)"
	cd backend && python -m alembic revision --autogenerate -m "Auto migration"
	@echo "$(GREEN)✅ 迁移文件已生成$(NC)"

migrate-reset: ## 重置数据库
	@echo "$(RED)⚠️ 重置数据库 (将删除所有数据)...$(NC)"
	@read -p "确定要重置数据库吗? [y/N] " confirm && [ "$$confirm" = "y" ]
	cd backend && python -m alembic downgrade base
	cd backend && python -m alembic upgrade head
	@echo "$(GREEN)✅ 数据库重置完成$(NC)"

# ================================
# Docker操作
# ================================

docker-up: ## 启动Docker开发环境
	@echo "$(BLUE)🐳 启动Docker开发环境...$(NC)"
	docker-compose up -d postgres redis
	@echo "$(GREEN)✅ Docker服务已启动$(NC)"
	@echo "$(YELLOW)PostgreSQL: localhost:5432$(NC)"
	@echo "$(YELLOW)Redis: localhost:6379$(NC)"

docker-down: ## 停止Docker服务
	@echo "$(BLUE)🐳 停止Docker服务...$(NC)"
	docker-compose down
	@echo "$(GREEN)✅ Docker服务已停止$(NC)"

docker-full: ## 完整Docker环境 (包括前后端)
	@echo "$(BLUE)🐳 启动完整Docker环境...$(NC)"
	docker-compose up -d
	@echo "$(GREEN)✅ 完整Docker环境已启动$(NC)"

docker-logs: ## 查看Docker服务日志
	@echo "$(BLUE)📋 查看Docker服务日志...$(NC)"
	docker-compose logs -f

docker-clean: ## 清理Docker资源
	@echo "$(BLUE)🧹 清理Docker资源...$(NC)"
	docker-compose down -v
	docker system prune -f
	@echo "$(GREEN)✅ Docker资源清理完成$(NC)"

# ================================
# 部署操作
# ================================

deploy-staging: ## 部署到测试环境
	@echo "$(BLUE)🚀 部署到测试环境...$(NC)"
	# 这里添加测试环境部署脚本
	@echo "$(GREEN)✅ 测试环境部署完成$(NC)"

deploy-prod: ## 部署到生产环境
	@echo "$(RED)🚀 部署到生产环境...$(NC)"
	@read -p "确定要部署到生产环境吗? [y/N] " confirm && [ "$$confirm" = "y" ]
	# 这里添加生产环境部署脚本
	@echo "$(GREEN)✅ 生产环境部署完成$(NC)"

# ================================
# 清理操作
# ================================

clean: ## 清理所有生成文件
	@echo "$(BLUE)🧹 清理生成文件...$(NC)"
	@echo "$(YELLOW)清理Python缓存...$(NC)"
	find . -type f -name "*.pyc" -delete
	find . -type d -name "__pycache__" -delete
	find . -type d -name "*.egg-info" -exec rm -rf {} +
	@echo "$(YELLOW)清理前端构建文件...$(NC)"
	cd frontend && rm -rf dist/ node_modules/.cache
	@echo "$(YELLOW)清理日志文件...$(NC)"
	rm -f *.log backend/*.log frontend/*.log
	@echo "$(GREEN)✅ 清理完成$(NC)"

clean-all: clean ## 清理所有文件包括依赖
	@echo "$(BLUE)🧹 清理所有文件...$(NC)"
	cd frontend && rm -rf node_modules/
	cd backend && pip freeze | grep -v "^-e" | xargs pip uninstall -y
	@echo "$(GREEN)✅ 深度清理完成$(NC)"

# ================================
# 实用工具
# ================================

logs: ## 查看应用日志
	@echo "$(BLUE)📋 查看应用日志...$(NC)"
	tail -f backend/app.log frontend/app.log 2>/dev/null || echo "$(YELLOW)日志文件不存在$(NC)"

health: ## 检查服务健康状态
	@echo "$(BLUE)🏥 检查服务健康状态...$(NC)"
	@echo "$(YELLOW)后端健康检查...$(NC)"
	curl -f http://localhost:8000/health || echo "$(RED)后端服务不可用$(NC)"
	@echo "$(YELLOW)前端健康检查...$(NC)"
	curl -f http://localhost:3000 || echo "$(RED)前端服务不可用$(NC)"

env-check: ## 检查环境配置
	@echo "$(BLUE)🔍 检查环境配置...$(NC)"
	@echo "$(YELLOW)Python版本:$(NC)"
	python --version
	@echo "$(YELLOW)Node.js版本:$(NC)"
	node --version
	@echo "$(YELLOW)NPM版本:$(NC)"
	npm --version
	@echo "$(YELLOW)Docker版本:$(NC)"
	docker --version || echo "$(RED)Docker未安装$(NC)"
	@echo "$(YELLOW)PostgreSQL连接:$(NC)"
	pg_isready -h localhost -p 5432 || echo "$(RED)PostgreSQL不可用$(NC)"

backup: ## 备份数据库
	@echo "$(BLUE)💾 备份数据库...$(NC)"
	pg_dump -h localhost -p 5432 -U postgres mindmate > backup_$(shell date +%Y%m%d_%H%M%S).sql
	@echo "$(GREEN)✅ 数据库备份完成$(NC)"

# ================================
# 开发助手
# ================================

api-docs: ## 打开API文档
	@echo "$(BLUE)📚 打开API文档...$(NC)"
	@echo "$(YELLOW)Swagger UI: http://localhost:8000/docs$(NC)"
	@echo "$(YELLOW)ReDoc: http://localhost:8000/redoc$(NC)"

monitor: ## 启动监控面板
	@echo "$(BLUE)📊 启动监控面板...$(NC)"
	# 这里可以启动监控工具
	@echo "$(YELLOW)监控面板: http://localhost:9090$(NC)"

generate-secret: ## 生成新的SECRET_KEY
	@echo "$(BLUE)🔐 生成新的SECRET_KEY...$(NC)"
	python -c "import secrets; print('SECRET_KEY=' + secrets.token_urlsafe(32))"